package org.example.springboot137.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.minio.MinioClient;
import io.minio.RemoveObjectsArgs;
import lombok.extern.slf4j.Slf4j;
import org.example.springboot137.config.MyConfigYml;
import org.example.springboot137.entity.Auction;
import org.example.springboot137.entity.Auctions;
import org.example.springboot137.entity.Imagess;
import org.example.springboot137.entity.ResponseEntity;
import org.example.springboot137.service.AuctionService;
import org.example.springboot137.service.ImagesService;
import org.example.springboot137.util.AliOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin("*")
public class AuctionController {
    //注入OSS工具类
    @Autowired
    private AliOSSUtil ossUtil;
    @Autowired
    private AuctionService auctionService;
    @Autowired
    private MyConfigYml myConfigYml;
    @Autowired
    private ImagesService imagesService;
    @Autowired
    MinioClient minioClient;
    //新增文件
    @PostMapping("/upload")
    public Map upload(@RequestParam("file") MultipartFile file) throws IOException {
        Map result = new HashMap();
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        //调用上传文件到阿里云
        result.put("url", ossUtil.upload(file));
        map.put("data",result);
        return map;
    }
    @PostMapping("delImg")
    public Object delImg(@RequestBody Imagess imagess)throws Exception{
        QueryWrapper<Imagess> wrapper=new QueryWrapper<>();
        wrapper.eq("id",imagess.getId());
        System.out.println(imagess.getImg());
        List<Imagess> list = imagesService.list(wrapper);
        minioClient.removeObject("star",list.get(0).getImg());
        
        return imagesService.remove(wrapper);
    }
    @PostMapping("addImg")
    public Object addImg(@RequestBody Imagess imagess)throws Exception {
        //String imagePath = minioClient.presignedGetObject("star",imagess.getImg());
        return imagesService.save(imagess);
    }
    @PostMapping("setImg")
    public Object setImg(@RequestBody Imagess imagess)throws Exception{
        imagesService.updateById(imagess);
        //String imagePath = minioClient.presignedGetObject("star",imagess.getImg());
        return "http://localhost:9000/star/"+imagess.getImg();
    }
    @GetMapping("aelectAuction/{page}")
    public ResponseEntity getAuction(@PathVariable("page")int page,Integer pageSize,@RequestParam(required = false) String auctionName){
        pageSize=(pageSize==null||"".equals(pageSize)||pageSize>1000)? myConfigYml.getPageSize():pageSize;
        System.out.println("auctionName = " + auctionName);
        List<Auctions> lists=new ArrayList<>();
        PageHelper.startPage(page,pageSize);//设置分页
        List<Auction> list = auctionService.getFind(auctionName);//获取分页
        List<Imagess> imagesList=imagesService.selectImages();
        for(int i=0;i<list.size();i++){
            Auctions auctions=new Auctions();
            List<String> img=new ArrayList<>();
            for (int j=0;j<imagesList.size();j++){
                if (list.get(i).getAuctionId() == imagesList.get(j).getAid()) {
                  img.add(imagesList.get(j).getImg());
                }
            }
            auctions.setAuction(list.get(i));
            auctions.setImagess(img);
            lists.add(auctions);
        }
        PageInfo pageInfo = new PageInfo<>(list);//获取分页信息
        pageInfo.setList(lists);
        //response.getData().put("pageInfo",pageInfo);
        return new ResponseEntity().addDate("pageInfo",pageInfo);
    }
    @GetMapping("aelectAuctions/{page}")
    public ResponseEntity getAuctions(@PathVariable("page")int page,Integer pageSize,@RequestParam(required = false) String auctionName)throws Exception{
        pageSize=(pageSize==null||"".equals(pageSize)||pageSize>1000)? myConfigYml.getPageSize():pageSize;
        System.out.println("auctionName = " + auctionName);
        PageHelper.startPage(page,pageSize);//设置分页
        List<Integer> integerList=this.auctionService.mylistPage(auctionName);//duiyumylist连表id集合
        System.out.println("integerList = " + integerList);
        List<Auction> list = auctionService.mylist(integerList);
        PageInfo pageInfo = new PageInfo<>(integerList);//获取分页信息
//        for (Auction auction : list) {
//            List<Imagess> imagess = auction.getImagess();
//            for (Imagess imagess1 : imagess) {
//                String img = imagess1.getImg();
//                String imagePath = minioClient.presignedGetObject("star",img);
//                imagess1.setImg(imagePath);
//            }
//        }
        pageInfo.setList(list);
        //response.getData().put("pageInfo",pageInfo);
        return new ResponseEntity().addDate("pageInfo",pageInfo);
    }
    /**
     * 查询
     */
    @GetMapping("auctionList")
    public  Object auctionList(@RequestParam(value = "pageNo",defaultValue = "1") int pageNo, @RequestParam(value = "pageSize",defaultValue = "5")  int pageSize,@RequestParam(required = false) String auctionName){
        System.out.println("pageNo = " + pageNo);
        System.out.println("pageSize = " + pageSize);
        System.out.println("auctionName = " + auctionName);
        return auctionService.auctionList(pageNo, pageSize,auctionName);
    }
    /**
     * 新增
     */
    @PostMapping("insertA")
    public Object insertA(@RequestBody Auction auction){
        return auctionService.save(auction);
    }
    /**
     * 修改
     */
    @PostMapping("updateA")
    public Object updateA(@RequestBody Auction auction){
        System.out.println("修改");
        return auctionService.updateAuction(auction);
    }
    /**
     * 删除
     */
    @PostMapping("deleteA")
    public Object deleteA(@RequestBody Auction row){
        System.out.println("row = " + row.getAuctionId());
        return auctionService.deleteA((int)row.getAuctionId());
    }
    @GetMapping("selectImg")
    public Object selectImg(Integer aid)throws Exception{
        List<Imagess> imagesses = imagesService.selectImg(aid);
        for (Imagess imagess1 : imagesses) {
            String img = imagess1.getImg();
            String imagePath = minioClient.presignedGetObject("star",img);
            imagess1.setImg(imagePath);
        }
        return imagesses;
    }
    //修改时查询
    @GetMapping("updateSelect")
    public Auction updateAll(Integer auctionId) {
        return auctionService.updateAll(auctionId);
    }
}
